package com.caigou.modules.personal.ui.info

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.bean.UserBean
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.modules.personal.databinding.FragmentNickNameBinding
import com.caigou.modules.personal.viewmodel.PersonalInfoViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/10/09
 */
@AndroidEntryPoint
class NickNameFragment : BaseFragment() {

    private var _binding: FragmentNickNameBinding? = null
    private val binding get() = _binding!!

    private val personalInfoViewModel: PersonalInfoViewModel by viewModels()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentNickNameBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        binding.nicknameEtName.setText(AppBackgroundInfo.instance.userBean!!.nickName)
        binding.nicknameTvSave.setOnClickListener {
            val nickName = binding.nicknameEtName.text.toString()
            if (TextUtils.isEmpty(nickName)) {
                showToast("昵称不能为空")
                return@setOnClickListener
            }
            if (nickName.equals(AppBackgroundInfo.instance.userBean!!.nickName)) {
                (requireActivity() as PersonalInfoActivity).onBackPressed()
            } else {
                personalInfoViewModel.modifyNickName(AppBackgroundInfo.instance.userBean!!.userId, nickName)
            }
        }

        binding.nicknameTvBack.setOnClickListener {
            (requireActivity() as PersonalInfoActivity).onBackPressed()
        }
    }

    override fun onBack() {
        binding.nicknameEtName.setText(AppBackgroundInfo.instance.userBean!!.nickName)
    }

    override fun lazyLoad() {
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(personalInfoViewModel.nickNameLiveData, ::handleModifyNickName)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun handleModifyNickName(status: Resource<UserBean>) {
        when (status) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                if (!TextUtils.isEmpty(status.errorMsg)) {
                    showToast(status.errorMsg)
                } else {
                    showToast("修改昵称异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                if (status.data == null) {
                    return
                }

                AppBackgroundInfo.instance.userBean = status.data
                (requireActivity() as PersonalInfoActivity).onBackPressed()
            }
        }
    }
}